#include <iostream>
#include "BinaryLinkTree.h"

struct Student
{
    Student(int id,const std::string& name,int score):id(id),name(name),score(score){
        std::cout<<"构造函数!\n";
    }
    ~Student(){
        std::cout<<"析构函数!\n";
    }
    
    int id;
    std::string name;
    int score;
};

void ShowStudent(void*p)
{
    Student *s = (Student*)p;
    std::cout<<s->id<<" "<<s->name<<"  ";
}

void FreeStudent(void*p)
{
    Student *s = (Student*)p;
    delete s;
}

int CompareID(void*p1,void*p2)
{
    Student *s1 = (Student *)p1;
    Student *s2 = (Student *)p2;
    return s1->id - s2->id;
}

bool CompareName(void*p1,void*p2)
{
    Student *s1 = (Student *)p1;
    Student *s2 = (Student *)p2;
    return s1->name > s2->name;
}

int SearchIDByIndex(void* p1,int index)
{
    Student *stu = (Student*)p1;
    return stu->id - index;
}

void ShowFindStudent(void *stu)
{
    Student* *s = (Student**)stu;
    Student *p = *s;
    std::cout<<p->id<<":"<<p->name<<std::endl;
}

int main()
{
    BLTree tree;
    tree.SetCallbackCompareID(CompareID);
    // Student *stu1 = new Student(1,"张三",20);
    // tree.Insert(stu1);
    tree.Insert(new Student(2,"李四",95));
    tree.Insert(new Student(3,"王五",90));
    tree.Insert(new Student(4,"李二",93));
    tree.Insert(new Student(5,"赵六",94));
    tree.Insert(new Student(6,"孙七",88));
    tree.SetCallbackShowStudent(ShowStudent);
    tree.show();
    // tree.Remove((int*)5,(char*)"fang");
    // tree.Remove(6);
    // int *c = tree.Find(2);
    // std::cout<<*c<<std::endl;
    // *c = 99;

    // tree.show();
    tree.SetCallbackSearchByIndex(SearchIDByIndex);
    ElementType c = tree.FindByIndex(2);
    ShowFindStudent(c);
    return 0;
}